Bokeh Tutorial

1.7 Animate

Exercise: Animate the climate map

Create a loop that updates the data source of the climate_map through time (for every month and year). You'll need to use the bokeh-server.

Note: when using the output_server make sure that your data source doesn't contain nans


In [9]:
# Imports
import numpy as np
import netCDF4

from bokeh.plotting import vplot, hplot, cursession, output_server, show

from viz import climate_map, timeseries, legend, get_slice

In [10]:
# Data
data = netCDF4.Dataset('data/Land_and_Ocean_LatLong1.nc')
t = data.variables['temperature']

In [11]:
# Output option
output_server("climate")


Using saved session configuration for http://localhost:5006/
To override, pass 'load_from_config=False' to Session

In [12]:
# Plots
climate_map = climate_map()
timeseries = timeseries()
legend = legend()

In [13]:
# Create layout
map_legend = hplot(climate_map, legend)
layout = vplot(map_legend, timeseries)

In [14]:
# Show
show(layout)

In [15]:
# Select data source for climate_map
renderer = climate_map.select(dict(name="image"))
ds = renderer[0].data_source

In [16]:
# Create a loop that goes through month and year and updates the image data
import time

for year_index in np.arange(2000, 2015, 1):
    for month_index in np.arange(1, 13, 1):
        image = get_slice(t, year_index, month_index)
        ds.data["image"] = [image]
        cursession().store_objects(ds)
        time.sleep(0.2)

In [ ]: